package com.jintoufs.web.action.shiro.filter;

import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.user.User;
import com.jintoufs.service.customer.CustomerService;
import com.jintoufs.service.user.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.PathMatchingFilter;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 14-2-15
 * <p>Version: 1.0
 */
public class SysUserFilter extends PathMatchingFilter {

    @Autowired
    private UserService userService;

    @Override
    protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {

        String username = (String)SecurityUtils.getSubject().getPrincipal();
        Map<String, Object> params = new HashMap<String, Object>();
		params.put("userName", username);
		List<User> list = userService.queryByParams(params);
		if (list != null && list.size() > 0) {
            User user=list.get(0);
			request.setAttribute(Constants.CURRENT_USER, user);
		}
        
        return true;
    }
}
